package com.example.demo.service.impl;

import com.example.demo.entity.Notice;
import com.example.demo.exception.BusinessException;
import com.example.demo.exception.ErrorCode;
import com.example.demo.mapper.NoticeMapper;
import com.example.demo.service.NoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NoticeServiceImpl implements NoticeService {
    private static final Logger logger = LoggerFactory.getLogger(NoticeServiceImpl.class);

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public Map<String, Object> getNoticeList(int page, int size, String keyword, String createTimeRange) {
        logger.info("Getting notice list - page: {}, size: {}, keyword: {}, createTimeRange: {}",
                page, size, keyword, createTimeRange);

        // 构建查询参数
        Map<String, Object> params = new HashMap<>();
        params.put("keyword", keyword);

        // 处理时间范围参数 (格式: "yyyy-MM-dd,yyyy-MM-dd")
        if (createTimeRange != null && !createTimeRange.isEmpty()) {
            String[] timeRange = createTimeRange.split(",");
            if (timeRange.length == 2) {
                params.put("startTime", timeRange[0]);
                params.put("endTime", timeRange[1]);
            }
        }

        PageHelper.startPage(page, size);
        List<Notice> list = noticeMapper.selectNoticeList(params);
        PageInfo<Notice> pageInfo = new PageInfo<>(list);

        Map<String, Object> data = new HashMap<>();
        data.put("list", pageInfo.getList());
        data.put("totalCount", pageInfo.getTotal());
        logger.info("Retrieved {} notices", pageInfo.getTotal());
        return data;
    }

    @Override
    public Notice createNotice(String title, String content) {
        logger.info("Creating new notice - title: {}, content: {}", title, content);
        Notice notice = new Notice();
        notice.setTitle(title);
        notice.setContent(content);
        noticeMapper.insertNotice(notice);
        logger.info("Created notice with ID: {}", notice.getId());
        return notice;
    }

    @Override
    public boolean updateNotice(Long id, String title, String content) {
        logger.info("Updating notice - ID: {}, title: {}, content: {}", id, title, content);
        // 检查ID是否存在
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        List<Notice> list = noticeMapper.selectNoticeList(params);
        if (list.isEmpty()) {
            logger.error("Notice not found - ID: {}", id);
            throw new BusinessException(ErrorCode.NOTICE_NOT_FOUND);
        }
        Notice notice = new Notice();
        notice.setId(id);
        notice.setTitle(title);
        notice.setContent(content);
        boolean result = noticeMapper.updateNotice(notice) > 0;
        logger.info("Notice update result - ID: {}, success: {}", id, result);
        return result;
    }

    @Override
    public boolean deleteNotice(Long id) {
        logger.info("Deleting notice - ID: {}", id);
        boolean result = noticeMapper.deleteNotice(id) > 0;
        logger.info("Notice deletion result - ID: {}, success: {}", id, result);
        return result;
    }
}
